<template>
  <el-dialog :modal="false"
             :modal-append-to-bod="false"
             :visible.sync="dialogVisible"
             width="80%">
    <el-table
      :data="dataList"
      row-key="id"
      :header-cell-style="{background:'#F5F7FA',color:'#606266'}"
      highlight-current-row
      @current-change="handleCurrentChange"
      v-loading="dataListLoading"
      style="width: 100%;">
      <el-table-column
        prop="resName"
        header-align="center"
        align="center"
        label="餐厅">
      </el-table-column>
      <el-table-column
        prop="eatName"
        header-align="center"
        align="center"
        label="就餐类型">
      </el-table-column>
      <!--<el-table-column
        prop="discount"
        header-align="center"
        align="center"
        label="折扣比例">
      </el-table-column>-->
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="创建时间">
      </el-table-column>
    </el-table>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" @click.native="successHandle()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import {treeDataTranslate} from '@/utils'

  export default {
    data () {
      return {
        queryForm: {
          key: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        dialogVisible: false,
        currentRow: null
      }
    },
    components: {},
    activated () {
      this.getDataList()
    },
    methods: {
      init () {
        this.dialogVisible = true
        this.getDataList()
      },
      activated () {
        this.getDataList()
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/tra/resdiscount/list'),
          method: 'get',
          params: this.$http.adornParams({
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = treeDataTranslate(data.list)
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 点击确认之后的处理
      successHandle () {
        if (this.currentRow.type === 0) {
          this.$message.error('不可以选择餐厅哦！')
          return
        }
        this.dialogVisible = false
        this.$emit('resCallback', this.currentRow)
      },
      // 获取选中行数据
      handleCurrentChange (val) {
        this.currentRow = val
        this.currentRow.fatherName = this.getResName(val.parentId)
      },
      // 获取餐厅名称
      getResName (pid) {
        let father = null
        for (let item of this.dataList) {
          if (item.id === pid) {
            father = item
          }
        }
        return father.resName
      }
    }
  }
</script>

<style scoped>

</style>
